package org.railage.client.gui.main;

import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.BoxLayout;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.apache.log4j.Logger;
import org.railage.client.Client;
import org.railage.client.gui.tabs.AbstractTab;
import org.railage.client.gui.tabs.BoughtTicketsTab;
import org.railage.client.gui.tabs.EditTripsTab;
import org.railage.client.gui.tabs.EditRoutesTab;
import org.railage.client.gui.tabs.PassengersTab;
import org.railage.client.gui.tabs.ScheduleTab;
import org.railage.client.gui.tabs.EditStationsTab;
import org.railage.client.gui.tabs.TripSearchTab;
import org.railage.client.gui.tabs.TripsTab;

public class MainPanel extends JPanel {
	private static final long serialVersionUID = -995366852387042535L;

	private static final Logger log = Logger.getLogger(MainPanel.class);

	public MainPanel() {
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		add(tabbedPane);

		int role = Client.getInstance().getCurrentUser().getRole();

		ChangeListener changeListener = new ChangeListener() {
			public void stateChanged(ChangeEvent changeEvent) {
				JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent
						.getSource();
				int index = sourceTabbedPane.getSelectedIndex();
				log.debug("Tab changed to: "
						+ sourceTabbedPane.getTitleAt(index));
				AbstractTab tab = (AbstractTab) sourceTabbedPane
						.getComponentAt(index);
				tab.init();
			}
		};
		tabbedPane.addChangeListener(changeListener);

		if (role == 0) {
			// поиск поезда, проходящего от станции A до станции B
			// в заданный промежуток времени
			AbstractTab tripSearchTab = new TripSearchTab();
			tabbedPane.addTab("Trip search", null, tripSearchTab, null);

			// расписание поездов по станции
			AbstractTab scheduleTab = new ScheduleTab();
			tabbedPane.addTab("Schedule", null, scheduleTab, null);

			// купленные пользователем билеты
			AbstractTab boughtTicketsTab = new BoughtTicketsTab();
			tabbedPane.addTab("Bought tickets", null, boughtTicketsTab, null);

		} else if (role == 1) {
			// просмотр всех зарегистрированных на поезд пассажиров
			AbstractTab passengersTab = new PassengersTab();
			tabbedPane.addTab("Passengers", null, passengersTab, null);

			// просмотр всех поездов
			AbstractTab tripsTab = new TripsTab();
			tabbedPane.addTab("Trips", null, tripsTab, null);

			// добавление/удаление станций
			AbstractTab editStationsTab = new EditStationsTab();
			tabbedPane.addTab("Edit stations", null, editStationsTab, null);

			// добавление/удаление рейсов
			AbstractTab editTripsTab = new EditTripsTab();
			tabbedPane.addTab("Edit trips", null, editTripsTab, null);

			// добавление/удаление маршрутов
			AbstractTab editRoutesTab = new EditRoutesTab();
			tabbedPane.addTab("Edit routes", null, editRoutesTab, null);
		}
	}
}
